// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   q.java

package com.intellij.ide.license.impl;

import com.intellij.concurrency.JobScheduler;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.license.LicenseException;
import com.intellij.licensecommon.data.e;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationAdapter;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.text.SyncDateFormat;

import javax.swing.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.prefs.Preferences;

public abstract class ServerSource extends GeneralLicenseSource {
    private class c_ implements Future {

        private final Future a;
        private final b_ b;
        final ServerSource this$0;

        public boolean cancel(boolean flag) {
            boolean flag1 = a.cancel(flag);
            b.a();
            return flag1;
            Exception exception;
            exception;
            b.a();
            throw exception;
        }

        public boolean isCancelled() {
            return a.isCancelled();
        }

        public boolean isDone() {
            return a.isDone();
        }

        public Object get()
                throws InterruptedException, ExecutionException {
            return a.get();
        }

        public Object get(long l1, TimeUnit timeunit)
                throws InterruptedException, ExecutionException, TimeoutException {
            return a.get(l1, timeunit);
        }

        c_(Future future, b_ b_1) {
            this$0 = ServerSource.this;
            super();
            a = future;
            b = b_1;
        }
    }

    private class b_ implements Runnable {

        private final ab a;
        private final AtomicBoolean b;
        private final AtomicBoolean c;
        final ServerSource this$0;

        public void a() {
            if (!c.getAndSet(true) && !a.r())
                ServerSource.this.b(a.v(), a.u());
        }

        public void run() {
            ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {

                                                                          final b_ this$1;

                                                                          public void run() {
                                                                              if (b.getAndSet(true))
                                                                                  break MISSING_BLOCK_LABEL_238;
                                                                              if (!c.get()) {
                                                                                  g_ g_1 = a(a);
                                                                                  if (g_1 == g_.OK) {
                                                                                      h();
                                                                                      b(a);
                                                                                  } else {
                                                                                      s();
                                                                                      if (a.r()) {
                                                                                          if (g_1 != g_.DISCONNECTED) {
                                                                                              SwingUtilities.invokeLater(new Runnable() {

                                                                                                                             final _cls0 this$2;

                                                                                                                             public void run() {
                                                                                                                                 Messages.showErrorDialog("License server refused to verify permanent license. Ticket is revoked.", "Permanent Ticket Revoked");
                                                                                                                             }


                                                                                                                             {
                                                                                                                                 this$2 = _cls0.this;
                                                                                                                                 super();
                                                                                                                             }
                                                                                                                         }
                                                                                              );
                                                                                              a(a.v(), a.u(), true);
                                                                                              b(a.v(), false);
                                                                                          }
                                                                                      } else {
                                                                                          b(a.v(), g_1 == g_.DISCONNECTED);
                                                                                      }
                                                                                  }
                                                                              }
                                                                              b.set(false);
                                                                              break MISSING_BLOCK_LABEL_238;
                                                                              Exception exception;
                                                                              exception;
                                                                              b.set(false);
                                                                              throw exception;
                                                                          }


                                                                          {
                                                                              this$1 = b_.this;
                                                                              super();
                                                                          }
                                                                      }
            );
        }


        b_(ab ab1) {
            if (ab1 == null) {
                throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{
                        "0", "com/intellij/ide/license/impl/ServerSource$ProlongPeriodicTask", "<init>"
                }));
            } else {
                this$0 = ServerSource.this;
                super();
                b = new AtomicBoolean(false);
                c = new AtomicBoolean(false);
                a = ab1;
                return;
            }
        }
    }

    private static class h_ extends f_ {

        public String getMessage() {
            return "Stored license data is corrupt. License Revoked";
        }

        private h_() {
        }

    }

    private static class d_ extends f_ {

        public String getMessage() {
            return "No response from license server for more than 48 hours";
        }

        private d_() {
        }

    }

    private static class f_ extends Exception {

        public Throwable fillInStackTrace() {
            return this;
        }

        private f_() {
        }

    }

    protected static final class g_ extends Enum {

        public static final g_ OK;
        public static final g_ REFUSED;
        public static final g_ DISCONNECTED;
        private static final g_ a[];

        public static g_[] values() {
            return (g_[]) a.clone();
        }

        public static g_ valueOf(String s1) {
            return (g_) Enum.valueOf(com / intellij / ide / a / f / q$g_, s1);
        }

        static {
            OK = new g_("OK", 0);
            REFUSED = new g_("REFUSED", 1);
            DISCONNECTED = new g_("DISCONNECTED", 2);
            a = (new g_[]{
                    OK, REFUSED, DISCONNECTED
            });
        }

        private g_(String s1, int i1) {
            super(s1, i1);
        }
    }


    private static final Logger c = Logger.getInstance("#com.intellij.ide.license.impl.ServerSource");
    private static final long d = 0xa4cb800L;
    private static final SyncDateFormat e = new SyncDateFormat(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"));
    private static final int f = 150;
    private final AtomicBoolean g = new AtomicBoolean(false);
    private final UUID h;
    private final int i;
    private final GeneralLicenseManager j;
    protected String k;
    private volatile long l;
    private volatile c_ m;
    private ApplicationAdapter n;

    public ServerSource(o o1, int i1, GeneralLicenseManager generalLicenseManager1) {
        super(o1);
        l = 0L;
        i = i1;
        j = generalLicenseManager1;
        h = o1.f();
    }

    private static String a(String s1, String s2, long l1, long l2, String s3, String s4,
                            String s5) {
        StringBuilder stringbuilder = new StringBuilder();
        stringbuilder.append(s1);
        stringbuilder.append(l2);
        if (l1 != 0L)
            stringbuilder.append(l1);
        stringbuilder.append(s3);
        stringbuilder.append(s2);
        stringbuilder.append(s4);
        stringbuilder.append(s5);
        int i1 = stringbuilder.toString().hashCode();
        return Integer.toHexString(i1);
    }

    protected static void a(String s1, String s2) {
        if (s1 == null)
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{
                    "0", "com/intellij/ide/license/impl/ServerSource", "showError"
            }));
        if (s2 == null)
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{
                    "1", "com/intellij/ide/license/impl/ServerSource", "showError"
            }));
        if (s2.length() > 150)
            s2 = (new StringBuilder()).append(s2.substring(0, 150)).append("...").toString();
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), s2, s1, 0);
    }

    private static String a(String s1) {
        if (s1 != null && s1.endsWith("/"))
            return s1.substring(0, s1.length() - 1);
        else
            return s1;
    }

    public void b(String s1) {
        k = s1;
    }

    public String g() {
        return k;
    }

    public void c() {
        try {
            b(a(false, true));
        } catch (LicenseException l1) {
        }
    }

    public void a()
            throws com.intellij.ide.license.LicenseException {
        b(a(true, false));
    }

    private ab a(boolean flag, boolean flag1)
            throws LicenseException {
        try {
            ab ab1 = a(k, true);
            if (ab1 != null)
                return ab1;
        } catch (h_ h_1) {
            throw new LicenseException("License data corrupt", h_1.getMessage());
        } catch (f_ f_1) {
        }
        if (k != null)
            try {
                ab ab2 = c(k);
                h();
                ab2.b(flag);
                ab2.a(flag1);
                return ab2;
            } catch (j j1) {
                c.info(IdeBundle.message("message.server.protocol.hijacked", new Object[0]));
                throw new LicenseException("Server protocol error", IdeBundle.message("message.server.protocol.hijacked", new Object[0]));
            } catch (h h1) {
                c.info("No free ticket available");
                throw new LicenseException("No free ticket available");
            } catch (i i1) {
                String s1 = null;
                try {
                    ab ab3 = a(k, false);
                    if (ab3 != null)
                        return ab3;
                } catch (d_ d_1) {
                    s1 = d_1.getMessage();
                } catch (f_ f_2) {
                }
                c.info(b(i1, s1));
                if (flag)
                    a(i1, s1);
            }
        return null;
    }

    private static void a(i i1, String s1) {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), b(i1, s1), "Unable to Contact Server", 0);
    }

    private static String b(i i1, String s1) {
        StringBuilder stringbuilder = new StringBuilder("Unable to contact license server");
        String s2 = i1.getMessage();
        if (!StringUtil.isEmptyOrSpaces(s2))
            stringbuilder.append(": ").append(s2);
        if (s1 != null)
            stringbuilder.append("; ").append(s1);
        return stringbuilder.toString();
    }

    protected void h() {
        l = System.currentTimeMillis();
    }

    public void b() {
    }

    protected String i() {
        return (new StringBuilder()).append("JetBrains.").append(o()).append(".PermanentLicense").toString();
    }

    protected String j() {
        return (new StringBuilder()).append("JetBrains.").append(o()).append(".LicenseServerTicket").toString();
    }

    protected ab a(String s1, String s2, long l1, Map map) {
        String s3 = (String) map.get(l());
        Date date;
        try {
            date = s3 != null ? e.parse(s3) : null;
        } catch (ParseException parseexception) {
            date = null;
        }
        int i1 = 0;
        try {
            i1 = Integer.parseInt((String) map.get(m()));
        } catch (NumberFormatException numberformatexception) {
        }
        String s4 = (String) map.get(k());
        if (s4 == null)
            s4 = "N/A";
        return licenseFactory.a(s2, l1, s1, s4, i1, date);
    }

    protected abstract String k();

    protected abstract String l();

    protected abstract String m();

    protected abstract String n();

    protected final UUID o() {
        return h;
    }

    protected final int p() {
        return i;
    }

    protected abstract ab c(String s1)
            throws h, i, j;

    protected abstract g_ a(ab ab1);

    protected abstract boolean b(String s1, String s2);

    protected void b(ab ab1) {
        Properties properties;
        ByteArrayOutputStream bytearrayoutputstream;
        long l1 = ab1.r() ? 0L : l;
        String s1 = a(ab1.v(), n(), l1, ab1.t(), ab1.g(), ab1.u(), String.valueOf(ab1.s()));
        properties = new Properties();
        properties.setProperty("server.url", ab1.v());
        properties.setProperty("prolongation.period", String.valueOf(ab1.t()));
        properties.setProperty("machine.id", n());
        properties.setProperty("ticket.id", ab1.u());
        properties.setProperty(k(), ab1.g());
        properties.setProperty("signature", s1);
        properties.setProperty(m(), String.valueOf(ab1.s()));
        Date date = ab1.q();
        if (date != null)
            properties.setProperty(l(), String.valueOf(date));
        if (!ab1.r())
            properties.setProperty("timestamp", String.valueOf(l1));
        bytearrayoutputstream = new ByteArrayOutputStream();
        properties.store(bytearrayoutputstream, "License server ticket information. Please do not alter this data");
        try {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(ab1.r() ? i() : j(), bytearrayoutputstream.toByteArray());
        } catch (IOException ioexception) {
            c.error(ioexception);
        }
        break MISSING_BLOCK_LABEL_382;
        IOException ioexception1;
        ioexception1;
        c.error(ioexception1);
        try {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(ab1.r() ? i() : j(), bytearrayoutputstream.toByteArray());
        } catch (IOException ioexception2) {
            c.error(ioexception2);
        }
        break MISSING_BLOCK_LABEL_382;
        Exception exception;
        exception;
        try {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(ab1.r() ? i() : j(), bytearrayoutputstream.toByteArray());
        } catch (IOException ioexception3) {
            c.error(ioexception3);
        }
        throw exception;
    }

    private ab a(String s1, boolean flag)
            throws f_ {
        if (s1 == null)
            return null;
        byte abyte0[] = Preferences.userRoot().getByteArray(flag ? i() : j(), null);
        if (abyte0 == null)
            return null;
        Properties properties = new Properties();
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
        try {
            properties.load(bytearrayinputstream);
            bytearrayinputstream.close();
        } catch (IOException ioexception) {
            return null;
        }
        String s2 = properties.getProperty("server.url");
        if (!Comparing.equal(a(s1), a(s2)))
            return null;
        String s3 = properties.getProperty("machine.id");
        String s4 = properties.getProperty("signature");
        String s5 = properties.getProperty("ticket.id");
        String s6 = properties.getProperty(k());
        String s7 = properties.getProperty(m());
        String s8 = properties.getProperty(l());
        String s9 = properties.getProperty("timestamp");
        long l1;
        try {
            l1 = Long.parseLong(properties.getProperty("prolongation.period"));
        } catch (NumberFormatException numberformatexception) {
            l1 = 0x927c0L;
        }
        long l2 = 0L;
        if (s9 != null)
            try {
                l2 = Long.parseLong(s9);
                l = Math.max(l2, l);
            } catch (NumberFormatException numberformatexception1) {
            }
        if (!flag && System.currentTimeMillis() - l2 > 0xa4cb800L) {
            a(s1, s5, false);
            throw new d_();
        }
        String s10 = a(s2, s3, l2, l1, s6, s5, s7);
        if (!s10.equals(s4) || !s3.equals(n())) {
            a(s1, s5, flag);
            throw new h_();
        }
        HashMap hashmap = new HashMap();
        hashmap.put(k(), s6);
        hashmap.put(m(), s7);
        if (s8 != null)
            hashmap.put(l(), s8);
        ab ab1 = a(s2, s5, l1, ((Map) (hashmap)));
        ab1.c(flag);
        return ab1;
    }

    protected void a(String s1, String s2, boolean flag) {
        b(s1, s2);
        Preferences.userRoot().remove(flag ? i() : j());
    }

    protected void c(ab ab1) {
        b(ab1);
        b_ b_1 = new b_(ab1);
        java.util.concurrent.ScheduledFuture scheduledfuture = JobScheduler.getScheduler().scheduleWithFixedDelay(b_1, ab1.t(), ab1.t(), TimeUnit.MILLISECONDS);
        m = new c_(scheduledfuture, b_1);
    }

    public ab q() {
        return (ab) super.d();
    }

    public void r() {
        s();
        ab ab1 = q();
        if (ab1 != null && !ab1.r())
            b(((LicenseCorruptException) (null)));
    }

    protected void s() {
        c_ c_1 = m;
        if (c_1 != null) {
            m = null;
            c_1.cancel(false);
        }
    }

    void b(final String serverUrl, final boolean delayed) {
        if (g.getAndSet(true))
            return;
        Runnable runnable = new Runnable() {

            final String val$serverUrl;
            final boolean val$delayed;
            final ServerSource this$0;

            public void run() {
                final Application app = ApplicationManager.getApplication();
                app.executeOnPooledThread(new Runnable() {

                                              final Application val$app;
                                              final _cls0 this$1;

                                              public void run() {
                                                  final Ref newLicenseRef = new Ref(null);
                                                  final Ref errorRef = new Ref(null);
                                                  try {
                                                      ab ab1 = c(serverUrl);
                                                      ServerSource.this.h();
                                                      newLicenseRef.set(ab1);
                                                  } catch (i i1) {
                                                      errorRef.set(i1);
                                                  } catch (j j1) {
                                                      errorRef.set(j1);
                                                  } catch (h h1) {
                                                      errorRef.set(h1);
                                                  }
                                                  app.invokeLater(new Runnable() {

                                                      final Ref val$errorRef;
                                                      final Ref val$newLicenseRef;
                                                      final _cls1 this$2;

                                                      public void run() {
                                                          g.set(false);
                                                          b b1 = (b) errorRef.get();
                                                          boolean flag = false;
                                                          if (b1 instanceof i) {
                                                              if (delayed) {
                                                                  ab ab2 = null;
                                                                  String s1 = null;
                                                                  try {
                                                                      ab2 = a(serverUrl, false);
                                                                  } catch (d_ d_1) {
                                                                      s1 = d_1.getMessage();
                                                                  } catch (f_ f_1) {
                                                                  }
                                                                  newLicenseRef.set(ab2);
                                                                  if (ab2 == null) {
                                                                      q.a((i) b1, s1);
                                                                      flag = true;
                                                                  }
                                                              }
                                                          } else if (b1 instanceof j) {
                                                              f().e();
                                                              flag = true;
                                                          } else if (b1 instanceof h) {
                                                              f().f();
                                                              flag = true;
                                                          }
                                                          ab ab3 = (ab) newLicenseRef.get();
                                                          if (ab3 != null) {
                                                              ServerSource.this.b(ab3);
                                                              c(ab3);
                                                          } else {
                                                              if (!flag)
                                                                  f().d();
                                                              _fld0.j.b(new com.intellij.ide.license.p.a_() {

                                                                  final _cls2 this$3;

                                                                  public void proceed() {
                                                                  }

                                                                  public void cancel() {
                                                                      GeneralLicenseManager _tmp = j;
                                                                      GeneralLicenseManager.G();
                                                                  }


                                                                  {
                                                                      this$3 = _cls2.this;
                                                                      super();
                                                                  }
                                                              }
                                                                      , null);
                                                          }
                                                      }


                                                      {
                                                          this$2 = _cls1.this;
                                                          errorRef = ref;
                                                          newLicenseRef = ref1;
                                                          super();
                                                      }
                                                  }
                                                          , ModalityState.NON_MODAL);
                                              }


                                              {
                                                  this$1 = _cls0.this;
                                                  app = application;
                                                  super();
                                              }
                                          }
                );
            }


            {
                this$0 = ServerSource.this;
                serverUrl = s1;
                delayed = flag;
                super();
            }
        };
        if (delayed)
            JobScheduler.getScheduler().schedule(runnable, 900L, TimeUnit.SECONDS);
        else
            runnable.run();
    }

    public void t() {
        if (n == null) {
            ApplicationAdapter applicationadapter = new ApplicationAdapter() {

                final ServerSource this$0;

                public void applicationExiting() {
                    s();
                }


                {
                    this$0 = ServerSource.this;
                    super();
                }
            };
            n = applicationadapter;
            ApplicationManager.getApplication().addApplicationListener(applicationadapter);
        }
        s();
        c(q());
    }

    public final boolean e() {
        return false;
    }

    public volatile LicenseCorruptException d() {
        return q();
    }


}
